package com.hockeo.client.ui.presenters;

import com.google.gwt.user.client.ui.Widget;
import com.hockeo.client.VHEventBus;
import com.hockeo.client.ui.views.MainView;
import com.hockeo.shared.VHUtil;
import com.hockeo.shared.rpc.command.IResponseReceiver;
import com.hockeo.shared.rpc.command.ReceiverCallback;
import com.hockeo.shared.rpc.command.user.commands.GetSessionInfo;
import com.hockeo.shared.rpc.command.user.responses.GotSessionInfo;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * Presenter that controls the main view.
 *
 * @version $Id$
 * @author jjanke
 */
@Presenter( view = MainView.class )
public class MainPresenter extends BasePresenter<MainView, VHEventBus> implements IResponseReceiver<GotSessionInfo>
{
  public void onStart()
  {
    // get the session info to know if a user is connected
    //
    VHUtil.dispatcher().dispatch( GetSessionInfo.INSTANCE, ReceiverCallback.get( this, null ) );
  }

  public void onChangeTopWidget( Widget w )
  {
    this.view.setTopWidget( w );
  }

  public void onChangeLeftWidget( Widget w )
  {
    this.view.setLeftWidget( w );
  }

  public void onChangeCenterWidget( Widget w )
  {
    this.view.setCenterWidget( w );
  }

  /**
   * Invoked when a new SessionInfo object is received from the server. Upon reception we
   * can display a login/logoff button and possibly show the name of the connected user.
   */
  @Override
  public void gotResponse( GotSessionInfo response, String strInfo )
  {
    this.eventBus.receivedSessionInfo( response.get() );
  }
}
